import Enums from '@/const/Enums'
import Util from '@/util/Util'
export default {
    getActivityTitle(cartItem: CartProductItem[]): string {
        let res: string = ''
        let activity = cartItem[0].activityInfo!
        let totalPrice = cartItem
            .filter((v) => v.checked!)
            .map((v) => v.productInfo.price * v.count)
            .reduce((v1, v2) => v1 + v2, 0)
        if (activity.type == 1) {
            if (totalPrice >= activity.full) {
                res = `已满足【${activity.full}】元，已优惠${activity.amount}元`
            } else {
                res = `在购买【${activity.full - totalPrice}】元，享优惠${activity.amount}元`
            }
        } else if (activity.type == 2) {
            if (totalPrice >= activity.full) {
                res = `已满足【${activity.full}】元，已享受${activity.discount! * 10}折优惠`
            } else {
                res = `在购买【${activity.full - totalPrice}】元，享${activity.discount! * 10}折优惠`
            }
        } else if (activity.type == 3) {
            if (totalPrice >= activity.full) {
                res = `已满足【${activity.full}】元，已领取赠品`
            } else {
                res = `在购买【${activity.full - totalPrice}】元，享【${activity.full}元领取赠品】`
            }
        }
        return res
    },
    getActivitySubTitle(cartItem: CartProductItem[]): string {
        let res: string = ''
        let activity = cartItem[0].activityInfo!
        if (activity.type == 1) {
            res = `满${activity.full}元优惠${activity.amount}元`
        } else if (activity.type == 2) {
            res = `满${activity.full}元${Math.round(activity.discount! * 10)}折优惠`
        } else if (activity.type == 3) {
            res = `满${activity.full}元赠送${activity.giveProduct!.title}`
        }
        return res
    },
    getFavor(cartItem: CartProductItem[]): Array<Favor> {
        let res: Array<Favor> = []
        let activity = cartItem[0].activityInfo
        let totalPrice = cartItem
            .filter((v) => v.checked!)
            .map((v) => v.productInfo.price * v.count)
            .reduce((v1, v2) => v1 + v2, 0)

        if (activity) {
            if (totalPrice >= activity.full) {
                let favor: Favor = {
                    type: 1,
                    activity: activity,
                    amount: 0
                }
                if (activity.type == 1) {
                    favor.amount = activity.amount!
                } else if (activity.type == 2) {
                    favor.amount = Math.round((1 - activity.discount!) * totalPrice)
                }
                res.push(favor)
            }
        }
        cartItem
            .filter((v) => v.checked!)
            .forEach((item) => {
                if (item.coupons) {
                    let amount = -1
                    let userCoupon: UserCoupon | undefined
                    item.coupons
                        .filter((coupon) => coupon.receive)
                        .forEach((coupon) => {
                            let tpl = coupon.coupon
                            let tempAmount = 0
                            if (tpl.type == 1) {
                                tempAmount = tpl.amount
                            } else if (tpl.type == 2) {
                                tempAmount = Math.round((1 - tpl.discount) * item.productInfo.price)
                            }
                            if (tempAmount > amount) {
                                amount = tempAmount
                                userCoupon = coupon
                            }
                        })
                    if (amount > -1) {
                        let favor: Favor = {
                            type: 2,
                            coupon: userCoupon,
                            amount: amount,
                            couponCartItem: item
                        }
                        res.push(favor)
                    }
                }
            })
        return res
    },
    hasActivityFavor(cartItem: CartProductItem[]): boolean {
        return this.getFavor(cartItem).findIndex((v) => v.type == 1) > -1
    },
    getTypeDesc(cartItem: CartProductItem[]): string {
        let activity = cartItem[0].activityInfo!
        let res = Util.getTextByEnum(Enums.ACTIVITY_TYPE, activity.type)
        return res
    }
}
